﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Services;
using Scopa.GamePlay;

namespace Scopa.Menu
{
    using Locale = ScopaLocale;

  public class DecorationPanel : BaseMenu
  {

    Button eder;
    Rectangle Anim, Anim2;
    Texture2D SideEdera, Back, BottomEdera, LeftSideEdera, foglia;
    public int colorBtnTimer, ShadowRoofEdera = 0, windEffect, windVelocity, direction = 1, directionLeaf = 1, counter = 0, semeCadente, x = -260, y = -300, shadow = 0, z = 0, RoofEderEdirection = 0;
    SpriteBatch spriteBatch;
    Rectangle windRec, windRec2, RroofEdera;
    Texture2D[] semi;
    Random rnd;
    Color color;

    public DecorationPanel(Game game, MenuDepths depth)
      : base(game, depth)
    {
      spriteBatch = Game.GetScopaContext().SpriteBatch;
      background = null;
    }


    public override void Initialize()
    {
      rnd = new Random(DateTime.Now.Second);
      semeCadente = rnd.Next(0, 1);

      windVelocity = 100;

      windRec = new Rectangle(windVelocity, -200, 50, 50);
      windRec2 = new Rectangle(rnd.Next(30, 600), -200, 32, 55);

      semi = new Texture2D[2];
      semi[0] = Game.Content.Load<Texture2D>(@"Images//Menu//AssoVolante");
      semi[1] = Game.Content.Load<Texture2D>(@"Images//Decks//Back");
      //semi[1] = Game.Content.Load<Texture2D>(@"Images//Menu//AssoVolante");
      //semi[2] = Game.Content.Load<Texture2D>(@"Images//Menu//AssoVolante");


      color = new Color(0, 0, 0);

      Anim = new Rectangle(-50, 100, 50, 50);
      RroofEdera = new Rectangle(500, -400, 230, 230);
      Anim2 = new Rectangle(550, 100, 140, 140);
      SideEdera = Game.Content.Load<Texture2D>(@"Images//Menu//SideEdera");
      BottomEdera = Game.Content.Load<Texture2D>(@"Images//Menu//BottomEdera");
      LeftSideEdera = Game.Content.Load<Texture2D>(@"Images//Menu//LeftSideEdera");
      foglia = Game.Content.Load<Texture2D>(@"Images//Menu//Foglia");

      eder = new Button(Game, @"Images//Menu//BottomEdera", new Rectangle(0 + y, 360, 180, 120), Color.Transparent, "small_font", "", Color.White);
      AddButton(eder, () =>
        {
          direction = -1;

        });

      base.Initialize();
    }


    public override void Update(GameTime gameTime)
    {

      if (x > 0)
      {
        direction = 0;

        if (color.G < 250)
          color.G += 2;
        else
        {
          if (shadow < 10)
            shadow += 1;

          if (Anim.X < 30)
            Anim.X += 5;
          else
          {
            z++;
            if (z > 11)
              z = 11;
          }

        }
      }

      if (windRec.X < -20)
      {
        windRec.X = rnd.Next(100, 700);
        windRec.Y = -rnd.Next(60, 200);
      }

      windRec.Y += 3;
      windRec.X -= 3;
      windEffect += directionLeaf * 1;


      if (windEffect > 40)
      {
        directionLeaf = -1;

        counter++;
      }

      if (windEffect == 0)
      {
          if (semeCadente == 1)
              semeCadente = 0;
          else
              semeCadente = 1;
      }

      if (windEffect < 0)
      {
        directionLeaf = 1;

      }
      windRec.Width = windEffect;

      if (counter > 3)
      {
        windRec2.Y += 5;

        windRec2.Width = windEffect;

      }

      if (windRec2.Y > 820)
      {
        counter = 0;
        semeCadente++;
        if (semeCadente == 2)
          semeCadente = 0;
        windRec2.Y = -100;
        windRec2.X = rnd.Next(100, 700);
      }

      if (RoofEderEdirection != 0)
      {
        if (RroofEdera.Y < -40)
          RroofEdera.Y += RoofEderEdirection * 4;
      }

      base.Update(gameTime);
    }


    public override void Draw(GameTime gameTime)
    {
      base.Draw(gameTime);

      spriteBatch.Begin();

      x += direction * 3;
      y += direction * 3;

      spriteBatch.Draw(SideEdera, new Rectangle(0 + x, 83 + shadow, 63, 250), new Color(0, 0, 0, 150));
      spriteBatch.Draw(SideEdera, new Rectangle(0 + x, 80 - shadow, 60, 250), color);
      spriteBatch.Draw(BottomEdera, new Rectangle(0 + y, 370 + shadow, 180, 120), new Color(0, 0, 0, 150));
      spriteBatch.Draw(BottomEdera, new Rectangle(0 + y, 367 - shadow, 180, 120), color);
      spriteBatch.Draw(LeftSideEdera, new Rectangle(700 - x, 43 + shadow / 3, 100, 370), new Color(0, 0, 0, 150));
      spriteBatch.Draw(LeftSideEdera, new Rectangle(700 - x, 45 - shadow / 3, 100, 370), color);
      //spriteBatch.Draw(LeftSideEdera, new Rectangle(0 - x, 45 + shadow / 3, 82, 107), null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 1.0f);

      spriteBatch.Draw(semi[semeCadente], windRec2, null, Color.White, 0.0f, new Vector2(semi[semeCadente].Bounds.Width / 2, semi[semeCadente].Bounds.Height / 2), SpriteEffects.None, 1.0f);
      spriteBatch.Draw(foglia, windRec, null, Color.White, 0.0f, new Vector2(foglia.Bounds.Width / 2, foglia.Bounds.Height / 2), SpriteEffects.None, 1.0f);

      spriteBatch.End();
    }

  }
}
